/**
 * 
 */
package com.vision.sales.cm.db.data;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import net.sf.gilead.pojo.gwt.LightEntity;

import com.vision.core.cm.data.IHasSystemTime;
import com.vision.sales.cm.data.IHasSalesInvoice;
import com.vision.sales.cm.data.TransactionStatus;


/**
 * @author Mark
 *
 */
@Entity
@Table(name = DeliveryReceipt.TABLE)
public class DeliveryReceipt extends LightEntity implements Serializable, IHasSystemTime, IHasSalesInvoice {
	
	public static interface FieldSpecs extends Serializable {
		int DR_NO_MAX = 10;
		int REMARKS_MAX = 300;
	}
	
	private static final long serialVersionUID = -5379665424881702625L;
	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	public static final String TABLE = "delivery_receipt";
	
	public static final String COL_ID = "id";
	public static final String COL_DR_NO = "dr_no";
	public static final String COL_DATE = "date";
	public static final String COL_CUSTOMER_ID = "customer_id";
	public static final String COL_REMARKS = "remarks";
	public static final String COL_SALES_INVOICE_ID = "sales_invoice_id";
	public static final String COL_STATUS = "status";
	
	public static final String COL_SYS_CREATED = "sys_created";
	public static final String COL_SYS_LAST_MOD = "sys_last_mod";
	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO, generator="sdelivery_receipt")
	@SequenceGenerator(name = "sdelivery_receipt", sequenceName = "delivery_receipt_seq")
	@Column(name = COL_ID)
	private Long id;
	
	@Column(name = COL_DR_NO, nullable = false, unique = true, updatable = false, length = FieldSpecs.DR_NO_MAX)
	private String drNo;
	
	@Column(name = COL_DATE, nullable = false)
	@Temporal(TemporalType.DATE)
	private Date date;
	
	/**
	 * References the {@link Customer} for this {@link DeliveryReceipt}.
	 */
	// Many DeliveryReceipts may have the same Customer
	@ManyToOne
	@JoinColumn(name = COL_CUSTOMER_ID, nullable = false)
	private Customer customer;
	
	@Column(name = COL_REMARKS, nullable = true, length = FieldSpecs.REMARKS_MAX)
	private String remarks;

	/**
	 * References the {@link SalesInvoice} for this {@link DeliveryReceipt}, if already associated.
	 */
	// Many DeliveryReceipts may have the same SalesInvoice
	@ManyToOne
	@JoinColumn(name = COL_SALES_INVOICE_ID, nullable = true)
	private SalesInvoice salesInvoice;
	
	/**
	 * NOTE: Although {@link DeliveryReceipt} is technically not a "Transaction,"
	 * it can be voided.
	 */
	@Column(name = COL_STATUS, nullable = false)
	@Enumerated(EnumType.STRING)
	private TransactionStatus status;
	
	// For internal use %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	/**
	 * The server {@link Timestamp} this object was created.
	 */
	@Column(name = COL_SYS_CREATED, nullable = false)
	private Timestamp sysCreated;
	
	/**
	 * The server {@link Timestamp} this object was last modified.
	 */
	@Column(name = COL_SYS_LAST_MOD, nullable = false)
	private Timestamp sysLastModified;
	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	DeliveryReceipt() { } // For Serializable
	
	public DeliveryReceipt(Date date) {
		this.date = date;
	}
	
	@Override
	public boolean equals(Object object) {
		if (object instanceof DeliveryReceipt)
		{
			DeliveryReceipt o = (DeliveryReceipt) object;
			return id.equals(o.id);
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		return id == null ? super.hashCode() : id.hashCode();
	}
	
	@Override
	public String toString() {
		return id + ": " + drNo + "(" + date + ")";
	}
	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}	

	public String getDrNo() {
		return drNo;
	}

	public void setDrNo(String drNo) {
		this.drNo = drNo;
	}

	public Date getDate() {
		return date;
	}
	
	public void setDate(Date date) {
		this.date = date;
	}
	
	public Customer getCustomer() {
		return customer;
	}
	
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	
	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	public SalesInvoice getSalesInvoice() {
		return salesInvoice;
	}

	public void setSalesInvoice(SalesInvoice salesInvoice) {
		this.salesInvoice = salesInvoice;
	}
	
	public TransactionStatus getStatus() {
		return status;
	}
	
	public void setStatus(TransactionStatus status) {
		this.status = status;
	}

	public Timestamp getSysCreated() {
		return sysCreated;
	}

	public void setSysCreated(Timestamp sysCreated) {
		this.sysCreated = sysCreated;
	}

	public Timestamp getSysLastModified() {
		return sysLastModified;
	}

	public void setSysLastModified(Timestamp sysLastModified) {
		this.sysLastModified = sysLastModified;
	}
	
	
}
